<template>
    <div class="main">
        <el-row class="mb-1">
            <el-col :span="24" class="text-right">
                <el-button type="info" @click="synchronous(false)">点击将题库相同题目的修改同步到这个比赛的题目</el-button>
                <el-button type="danger" @click="synchronous(true)">点击将这里比赛题目配置的修改将同步题库相同题目</el-button>
            </el-col>
        </el-row>
        <el-table border :data="tableData">
            <el-table-column prop="paixu" width="80px" type="index"></el-table-column>
            <el-table-column prop="name" label="name"></el-table-column>
            <el-table-column prop="type" label="类型"></el-table-column>
            <el-table-column prop="updatedAt" width="120px" label="更新时间">

            </el-table-column>

            <el-table-column label="操作" fixed="right" width="230">
                <template #default="scope">
                    <router-link v-slot="{ navigate }" custom :to="{ name: '查看比赛题目', query: { ID: scope.row.ID } }">
                        <el-button @click="navigate" type="success" size="default">修改</el-button>
                    </router-link>
                    <el-button size="default" @click="del(scope.row.ID)" type="danger">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus';
import { computed, onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { asyncdelmatchquestion, asyncselectmatchquestion, asyncsynchronous } from '../../../api';
import Studycodemodel from '../../../mysql_interface/studycodemodel';

const route = useRoute()

const ID = computed(() => {
    return route.params.ID as string
})

async function del(ID: number) {
    ElMessageBox.confirm("是否删除该选项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    })
        .then(async () => {
            await asyncdelmatchquestion({ ID });
            ElMessage({
                type: "success",
                message: "删除成功!",
            });
            start();
        })
        .catch(() => {
            ElMessage({
                type: "info",
                message: "已取消删除",
            });
        });
}

const tableData = ref<Studycodemodel.Imatchquestion[]>([])

async function start() {
    const [data] = await asyncselectmatchquestion({ activityid: Number(ID.value) })
    tableData.value = data.matchquestion?.sort((a, b) => Number(a.paixu) - Number(b.paixu)) || []
}


async function synchronous(t: boolean) {
    console.log("synchronous:", ID.value, t)
    let title = t ? '是否将比赛题目同步到普通题目, 是否继续?' : '是否将普通题目同步到比赛题目, 是否继续?'
    ElMessageBox.confirm(title, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    }).then(async e => {
        await asyncsynchronous({ activityid: Number(ID.value), t })
        ElMessage.success('同步成功')
    }).catch(c => {

    })
}


onMounted(() => {
    start()
})

</script>

<style>
.main {
    min-height: 100%;
}
</style>